import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';

import {AuthService} from '../../service/auth.service'

import {LoginInfo} from '../../model/login-info'
import { Router } from '@angular/router';
 
@Component({
  selector: 'app-login-page',
  templateUrl: './login-page.component.html',
  styleUrls: ['./login-page.component.css']
})
export class LoginPageComponent implements OnInit {

  constructor(
    private authService: AuthService,
    private router: Router) { }

  loginInfo: LoginInfo = new LoginInfo("", "")

  ngOnInit(): void {
  }

  

  onLoginSubmit(){
    console.log("onLoginSubmit")
    console.log(JSON.stringify(this.loginInfo))
    this.authService
    .postLoginBasicAuth(this.loginInfo.username, this.loginInfo.password)
    .subscribe((data: any) => {
      console.log(JSON.stringify(data))
      console.log(data.token)
      localStorage.setItem('token', data.token)
      this.router.navigateByUrl("login/me-page")
    }, error => {
      console.log("login error " + error.status.toString())
      console.log(error)
    })
  }

  ping(){
    console.log("THIS IS PINGGGGGGGG")
    this.authService.getHello().subscribe(data => {
      console.log(JSON.stringify(data))
    })
  }

}
